package cn.itsource.basic.controller;

import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.FastdfsUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/fastDfs")
public class FastDFSController {

    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file){
        //获取文件的后缀名
        //文件的原始名字
        String filename = file.getOriginalFilename();//4号技师.jpg
        String fileSuffix = filename.substring(filename.lastIndexOf(".")+1);
        System.out.println(fileSuffix);
        /*
           file: 文件的byte[]
           extName:  后缀名
         */
        try {
            //文件上传到fastDfs 返回 文件的存储路径
            String path = FastdfsUtil.upload(file.getBytes(), fileSuffix);
            //把路径返回给前端
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
